package org.dromara.system.controller.autocode;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.autocode.domain.entity.SysAutoCodePart;
import org.dromara.autocode.service.SysAutoCodePartService;
import org.dromara.common.core.domain.PageData;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 编码生成规则组成
 *
 * @author wenrui Su
 * @date 2025-03-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/autoCodePart")
public class SysAutoCodePartController extends BaseService {

  private final SysAutoCodePartService sysAutoCodePartService;

/**
 * 查询编码生成规则组成列表
 */
@SaCheckPermission("sys:autoCodePart:list")
@GetMapping("/list")
  public PageData list(SysAutoCodePart bo, PageQuery pageQuery) {
    return this.resPage(sysAutoCodePartService.queryPageList(bo, pageQuery));
  }

  /**
   * 导出编码生成规则组成列表
   */
  @SaCheckPermission("sys:autoCodePart:export")
  @Log(title = "编码生成规则组成", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(SysAutoCodePart bo, HttpServletResponse response) {
    List<SysAutoCodePart> list = sysAutoCodePartService.queryList(bo);
    ExcelUtil.exportExcel(list, "编码生成规则组成", SysAutoCodePart. class,response);
  }

  /**
   * 获取编码生成规则组成详细信息
   *
   * @param partId 主键
   */
  @SaCheckPermission("sys:autoCodePart:query")
  @GetMapping("/{partId}")
  public PageData getInfo(@NotNull(message = "主键不能为空") @PathVariable Long partId) {
    return this.resData(sysAutoCodePartService.queryById(partId));
  }

  /**
   * 新增编码生成规则组成
   */
  @SaCheckPermission("sys:autoCodePart:add")
  @Log(title = "编码生成规则组成", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public PageData add(@Validated(AddGroup.class) @RequestBody SysAutoCodePart bo) {
    return this.resData(sysAutoCodePartService.insertByBo(bo));
  }

  /**
   * 修改编码生成规则组成
   */
  @SaCheckPermission("sys:autoCodePart:edit")
  @Log(title = "编码生成规则组成", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public PageData edit(@Validated(EditGroup.class) @RequestBody SysAutoCodePart bo) {
    return this.resData(sysAutoCodePartService.updateByBo(bo));
  }

  /**
   * 删除编码生成规则组成
   *
   * @param partIds 主键串
   */
  @SaCheckPermission("sys:autoCodePart:remove")
  @Log(title = "编码生成规则组成", businessType = BusinessType.DELETE)
  @DeleteMapping("/{partIds}")
  public PageData remove(@NotEmpty(message = "主键不能为空")
                        @PathVariable Long[] partIds) {
    return this.resData(sysAutoCodePartService.deleteWithValidByIds(List.of(partIds), true));
  }
}
